import request from '@/utils/request';
import type { ApiResult, PageParam, PageResult } from '@/api';

enum API {
  pageList = '/sysJob/pageJobDetail',
  jobTrrigerList = '/sysJob/jobTriggerList',
  startAllJob = '/sysJob/startAllJob', //启动所有作业
  pauseAllJob = '/sysJob/pauseAllJob', //暂停所有作业
  cancelSleep = '/sysJob/cancelSleep', //强制唤醒作业调度器
  persistAll = '/sysJob/persistAll', //强制触发所有作业持久化
  startJob = '/sysJob/startJob', //启动作业
  pauseJob = '/sysJob/pauseJob', //暂停作业
  deleteJob = '/sysJob/deletJobDetail', //删除作业
  pauseTrigger = '/sysJob/pauseTrigger', //暂停触发器
  SetTimerStatus = '/sysTimer/setStatus',
  TimerPageList = '/sysTimer/page',
  startTrigger = '/sysJob/startTrigger' //启动触发器
}

// 获取作业分页列表
export const getJobPageList = async (params?: PageParam) => {
  const res = await request.post<ApiResult<PageResult<any>>>(
    API.pageList,
    params
  );
  if (res.data.code === 200) {
    return res.data.result;
  }
  return Promise.reject(new Error(res.data.message));
};

// 获取触发器列表
export const getJobTriggerList = async (params?: any) => {
  const res = await request.get<ApiResult<any>>(API.jobTrrigerList, params);
  if (res.data.code === 200) {
    return res.data.result;
  }
  return Promise.reject(new Error(res.data.message));
};

// 启动所有作业
export const startAllJob = async () => {
  const res = await request.post<ApiResult<any>>(API.startAllJob);
  if (res.data.code === 200) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
};

//暂停所有作业
export const pauseAllJob = async () => {
  const res = await request.post<ApiResult<any>>(API.pauseAllJob);
  if (res.data.code === 200) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
};

//强制唤醒作业调度器
export const cancelSleep = async () => {
  const res = await request.post<ApiResult<any>>(API.cancelSleep);
  if (res.data.code === 200) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
};

//强制触发所有作业持久化
export const persistAll = async () => {
  const res = await request.post<ApiResult<any>>(API.persistAll);
  if (res.data.code === 200) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
};

//启动作业
export const startJob = async (params?: any) => {
  const res = await request.post<ApiResult<any>>(API.startJob, params);
  if (res.data.code === 200) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
};

//暂停作业
export const pauseJob = async (params?: any) => {
  const res = await request.post<ApiResult<any>>(API.pauseJob, params);
  if (res.data.code === 200) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
};

//删除作业
export const deleteJob = async (params?: any) => {
  const res = await request.post<ApiResult<object>>(API.deleteJob, params);
  if (res.data.code === 200) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
};

//启动触发器
export const startTrigger = async (params?: any) => {
  const res = await request.post<ApiResult<any>>(API.startTrigger, params);
  if (res.data.code === 200) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
};

//暂停触发器
export const pauseTrigger = async (params?: any) => {
  const res = await request.post<ApiResult<any>>(API.pauseTrigger, params);
  if (res.data.code === 200) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
};

// 设置定时任务状态
export const setTimerStatus = async (timerName: string, status: number) => {
  const res = await request.post<ApiResult<any>>(API.SetTimerStatus, {
    timerName,
    status
  });
  if (res.data.code === 200) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
};

export const getTimerPageList = async (params?: any) => {
  const res = await request.get<ApiResult<any>>(API.TimerPageList, {
    params
  });
  if (res.data.code === 200) {
    return res.data.result;
  }
  return Promise.reject(new Error(res.data.message));
};

export const add = async (data?: any) => {
  const res = await request.post<ApiResult<any>>('/sysJob/addJobDetail', data);
  if (res.data.code === 200) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
};

export const update = async (data?: any) => {
  const res = await request.post<ApiResult<any>>(
    '/sysJob/updateJobDetail',
    data
  );
  if (res.data.code === 200) {
    return res.data.message;
  }
  return Promise.reject(new Error(res.data.message));
};
